package com.polaris.live.common.util

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.polaris.live.common.AppModule
import com.polaris.live.common.R
import java.lang.ref.WeakReference

/**
 * 全局toast  可选择展示时间的长短  中间还是底部   后续需要可以自定义
 *
 * @author Created by 半仙 on 2023/8/15/015 12:03
 */
object ToastGravity {

    private var weakToast: WeakReference<Toast>? = null

    //=======================================================中间toast========================================================================

    fun showGravity(@StringRes strId: Int) {
        showGravity(StringUtils.getString(strId), false)
    }

    fun showGravity(myString: String?) {
        showGravity(myString, false)
    }

    fun showGravity(myString: String?, showLong: Boolean) {
        if (!TextUtils.isEmpty(myString)) {
            showMessage(myString, showLong)
        }
    }

    //======================================================底部吐司===============================================================================
    fun showDownGravity(myString: String?, showLong: Boolean? = false) {
        showDownGravity(myString, showLong)
    }

    private fun showDownGravity(myString: String?, showLong: Boolean) {
        if (!TextUtils.isEmpty(myString)) {
            ToastUtils.make().setBgResource(R.drawable.default_toast_bg)
                .setGravity(Gravity.BOTTOM, 0, 0)
                .setTextSize(14)
                .setTextColor(Color.parseColor("#FFFFFF"))
                .setDurationIsLong(showLong)
                .show(myString)
        }
    }

    private fun showMessage(msg: CharSequence?, showLong: Boolean? = null) {
        cancel()

        val duration = when {
            showLong == true || msg != null && msg.length > 20 -> Toast.LENGTH_LONG
            else -> Toast.LENGTH_SHORT
        }

        val context = AppModule.mApplication

        val toast = Toast.makeText(context, "", duration)
        val v = inflateToastView(context, R.layout.layout_base_toast_message)
        val tv = v.findViewById<TextView>(R.id.tv_toast)
        tv.text = msg

        toast.setGravity(Gravity.BOTTOM, 0, 600)

        if (SystemUtils.isSdkInt30()) {
            toast.addCallback(object : Toast.Callback() {
                override fun onToastHidden() {
                    cancel()
                }
            })
        }

        toast.view = v
        toast.show()

        weakToast = WeakReference(toast)
    }

    private fun cancel() {
        weakToast?.get()?.cancel()
        weakToast = null
    }

    private fun inflateToastView(context: Context, layoutResId: Int): View {
        return LayoutInflater.from(context).inflate(layoutResId, null)
    }
}